﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pin : MonoBehaviour {

	public float speed = 5;
	/// <summary>
	/// 不需要飞
	/// </summary>
	private bool isFly = false;
	/// <summary>
	/// 没有到达目标位置
	/// </summary>
	private bool isReach = false;
	private Transform startPoint;
	/// <summary>
	/// 目标圆的位置
	/// </summary>
	private Transform circle;
	private Vector3 targetCirclePos;

	void Start () {
		startPoint = GameObject.Find("StartPoint").transform;
		//circle = GameObject.Find("Circle").transform;
		circle = GameObject.FindGameObjectWithTag("Circle").transform;
		targetCirclePos = circle.position;
		targetCirclePos.y -= 1.77f;//显示在圆的边缘

	}
	
	// Update is called once per frame
	void Update () {
		//将等待的针移动到开始针
        if (isFly==false)
        {
            if (isReach==false)
            {
				//朝向目标位置移动   开始位置，结束位置，移动时间
				transform.position = Vector3.MoveTowards(transform.position,startPoint.position,speed * Time.deltaTime);
                // 到达目标位置  物理关节，距离关节，最重要的用处就是使两个物体保持一定距离
                if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
                {
                    isReach = true;
                }
            }
        }
		else
		{
			transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);
            if (Vector3.Distance(transform.position, targetCirclePos) < 0.05f)
            {
                transform.position = targetCirclePos;
				//跟随小球运动
                transform.parent = circle;
                isFly = false;
            }
        }
	}

	public void StartFly()
	{
		isFly = true;
		isReach = true;
	}
}
